import Vue from 'vue';
import VueRouter from 'vue-router';
import constantRoutes from './constant';
import { createGuard } from './guard';
import { asyncRoutes } from './routes';

Vue.use(VueRouter);

const routes = [
  ...constantRoutes
];

const createRouter = () => new VueRouter({
  base: '',
  mode: 'hash',
  scrollBehavior: () => ({ y: 0 }),
  routes
});

const router = createRouter();

createGuard(router);
// 解决跳转当前路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};

export { asyncRoutes, constantRoutes };
export default router;
